package com.zgkxzx.mvvmbase.view.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

/**
 * 创建者 zgkxzx
 * 日期   2/2/18
 * 描述
 */

public abstract class BaseAdapter<BIV extends ViewDataBinding, T> extends RecyclerView.Adapter<BaseViewHolder<BIV>> {
    private static final String TAG = BaseAdapter.class.getSimpleName();

    protected Context context;
    protected List<T> list;
    private LayoutInflater inflater;


    public BaseAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BIV viewBinding = DataBindingUtil.inflate(inflater, getItemLayoutId(), parent, false);
        return new BaseViewHolder(viewBinding);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        bindItemView(context, (BIV) holder.viewBinding, position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * 获取Item布局资源
     *
     * @return
     */
    protected abstract int getItemLayoutId();


    /**
     * 绑定Item View
     *
     * @param viewBinding
     * @param position
     */
    protected abstract void bindItemView(Context context, BIV viewBinding, int position);

}
